<template>
  <div class="app-container">
<!--    搜索框-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺类型" prop="address">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择店铺类型"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in options"
            :label="item.fName"
            :value="item.fId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入店铺地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商铺ID" align="center" prop="id" />
      <el-table-column label="店铺名称" align="center" prop="name" #default="scan">
        <a @click="handleAdd(scan.row.id)" style="color: #00afff">
          {{scan.row.name}}
        </a>
      </el-table-column>
      <el-table-column label="店铺类型" align="center" prop="typeName" />
      <el-table-column label="店铺状态" align="center" prop="status" #default="Sstatus">
        <span v-if="Sstatus.row.status == 0">正常</span>
        <span v-if="Sstatus.row.status == 1">封禁</span>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="leader.name" #default="sLeader">
          <span v-if="sLeader.row.leader != null">
            <a style="color: #00afff" @click="handleA(sLeader.row.leader.id)">{{sLeader.row.leader.name}}</a>
          </span>
        <span v-if="sLeader.row.leader == null">没有
        </span>
      </el-table-column>
      <el-table-column label="电话" align="center" prop="leader.phone" width="110"/>
      <el-table-column label="身份证号" align="center" prop="leader.idcard" width="140" />
      <el-table-column label="商铺营业额" align="center" prop="price" #default="Sprice">
        <a v-if="Sprice.row.price==null">
          0.00
        </a>
        <a v-if="Sprice.row.price!=null">
          {{Sprice.row.price}}.00
        </a>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="user" #default="sUser">
        <a @click="StoreA(sUser.row.id)" style="color: #00afff">
          {{sUser.row.user}}
        </a>
      </el-table-column>
      <el-table-column label="拼团" align="center" prop="group" #default="sGroup">
        <a @click="GroupA(sGroup.row.id)" style="color: #00afff">
          {{sGroup.row.group}}
        </a>
      </el-table-column>
      <el-table-column label="订单" align="center" prop="order" #default="sOrder">
        <a @click="OrderA(sOrder.row.id)" style="color: #00afff">
          {{sOrder.row.order}}
        </a>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="95"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row.id)"
            v-hasPermi="['system:store:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 店铺回显 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名称" prop="name" >
          <el-input v-model="form.name" placeholder="请输入店铺名称" disabled/>
        </el-form-item>
        <el-form-item label="店铺类型" prop="price" >
          <el-select
            v-model="form.type"
            placeholder="Select"
            size="small"
            style="width: 240px"
            disabled
          >
            <el-option
              v-for="item in options"
              :key="item.fId"
              :label="item.fName"
              :value="item.fId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="店铺状态" prop="address">
          <el-select
            v-model="form.status"
            placeholder="Select"
            size="small"
            style="width: 240px"
            disabled
          >
            <el-option
              v-for="item in option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="拼团点" prop="address">
          <el-select
            v-model="form.communityName"
            placeholder="Select"
            size="small"
            style="width: 240px"
            disabled
          >
            <el-option
              v-for="item in optionCommunity"
              :key="item.title"
              :label="item.title"
              :value="item.title"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入店铺地址" disabled/>
        </el-form-item>
        <el-form-item label="信用代码" prop="address" >
          <el-input v-model="form.idcard" placeholder="请输入社会信用代码" disabled/>
        </el-form-item>
        <el-form-item label="邀请码" prop="address">
          <el-input v-model="form.invitationCode" placeholder="请输入团长邀请码" disabled/>
        </el-form-item>
        <el-form-item label="照片" prop="address">
          <img :src="form.foodHygieneApproval" alt="" style="width: 60px;height: 60px">
          <img :src="form.frontIdCard" alt="" style="width: 60px;height: 60px">
          <img :src="form.license" alt="" style="width: 60px;height: 60px">
          <img :src="form.sideIdCard" alt="" style="width: 60px;height: 60px">
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 添加或修改管理对话框 -->
    <el-dialog :title="title" :visible.sync="openupdate" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="店铺类型" prop="price">
          <el-select
            v-model="form.type"
            placeholder="Select"
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="item in options"
              :key="item.fId"
              :label="item.fName"
              :value="item.fId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="店铺状态" prop="address">
          <el-select v-model="form.status" placeholder="Select" style="width: 240px">
            <el-option
              v-for="item in option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="拼团点" prop="address">
<!--          <el-input v-model="form.communityName" placeholder="请输入店铺状态" />-->
          <el-select
            v-model="form.communityName"
            placeholder="Select"
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="item in optionCommunity"
              :key="item.title"
              :label="item.title"
              :value="item.title"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入店铺地址" />
        </el-form-item>
        <el-form-item label="信用代码" prop="address">
          <el-input v-model="form.idcard" placeholder="请输入社会信用代码" />
        </el-form-item>
        <el-form-item label="邀请码" prop="address">
          <el-input v-model="form.invitationCode" placeholder="请输入团长邀请码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--团长回显-->
    <el-dialog :title="title" :visible.sync="openA" width="500px" append-to-body>
      <el-form ref="form" :model="wzy" :rules="rules" label-width="80px">
        <el-form-item label="负责人:" prop="name" >
          <el-input v-model="wzy.name" disabled/>
        </el-form-item>
        <el-form-item label="电话:" prop="name">
          <el-input v-model="wzy.phone" disabled/>
        </el-form-item>
        <el-form-item label="身份证号:" prop="name">
          <el-input v-model="wzy.idcard" disabled/>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

import {byName, listStore, Selectclass, SelectLeader, updateStore} from "@/api/system/store";
import {All} from "@/api/system/community";

export default {
  name: "Store",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管理表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openupdate: false,
      openA: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type: null,
        address: null,
        id:null,
        leaderId:null,
      //   拼团点id
        communityId:null,
      },
      // 表单参数
      form: {},
      wzy: {},
      options:{},
      option:[
        {
          value: 0,
          label: '正常',
        },
        {
          value: 1,
          label: '封禁',
        },
      ],
      optionCommunity: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.communityId=this.$route.params.CommunityId
    console.log(this.queryParams.communityId+"communityId")
    this.queryParams.name=this.$route.params.storeName
    console.log(this.queryParams.name)
    this.queryParams.leaderId=this.$route.params.leaderId
    console.log(this.queryParams.leaderId+"  leaderId")
    if(this.queryParams.leaderId !=null || this.queryParams.communityId!=null){
      this.queryParams.pageNum=1;
      this.queryParams.pageSize=1000;
    }
    this.getList();
    this.clas()
    this.Ocommunity();
  },
  methods: {
    /** 查询管理列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then(response => {
        var reg = /^(\d{4})\d+(\d{4})$/;
        response.data.list.forEach(item => {
          item.leader.idcard = item.leader.idcard.replace(reg, '$1********$2');
        });
        this.storeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: null,
        type: null,
        status: null,
        address: null,
        price: null,
        createTime: null,
        updateTime: null,
        invitationCode:null,
        idcard:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //下拉店铺类型
    clas(){
      Selectclass().then(res=>{
        this.options=res.data
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      location.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 回显店铺 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      byName(row).then(res=>{
        if(res.flag==true){
          this.form=res.data
          this.clas();
        }else{
          alert("失败")
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.clas();
      this.openupdate=true;
      byName(row).then(res=>{
        if(res.flag==true){
          this.form=res.data
        }else{
          alert("失败")
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除管理编号为"' + ids + '"的数据项？').then(function() {
        return delStore(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/store/export', {
        ...this.queryParams
      }, `store_${new Date().getTime()}.xlsx`)
    },
    //修改
    submitForm(){
      updateStore(this.form).then(response => {
        if(response.flag==true){
          this.openupdate=false;
          this.getList();
        }
      });
    },
    //团长回显
    handleA(row){
      SelectLeader(row).then(response => {
        if(response.flag==true){
          this.openA=true
          this.wzy=response.data
        }
      })
    },
    //跳转商家
    StoreA(row){
      this.$router.push({
        name: "Userb",
        query: { keyword: row },//这种方式的传参必须要有路由的别名name
      });
    },
    //添加拼团
    GroupA(row){
      this.$router.push({
        name: "GroupManage",
        query: { Group: row },//这种方式的传参必须要有路由的别名name
      });
    },
    //跳转订单
    OrderA(row){
      this.$router.push({
        name: "Ordera",
        query: { order: row },//这种方式的传参必须要有路由的别名name
      });
    },
    Ocommunity(){
      All().then(res=>{
        this.optionCommunity=res.data
      })
    }
  }
};
</script>
